//Parser File Format (w/o '|' symbol): |Tile Name|#|ID| |Number of Tiles| |Exits Randomized?|

#include "stdio.h"
#include <iostream>
#include <String>
#include <fstream>
#include <list>
#include "Tile.h"
#include "TileParser.h"
#include "random.h"
#include "GlobalStructs.h"
using namespace std;

//******************************	Public Functions	******************************

ParseTiles::ParseTiles(void) {
	m_readFile = "defaultTileData.txt";
	m_numTiles = 0;
	m_numAmount = 0;
}

ParseTiles::ParseTiles(char * a_readFile)
	:m_readFile(a_readFile), m_numTiles(0), m_numAmount(0) {
}

ParseTiles::~ParseTiles(void) {
}

int ParseTiles::getListSize(void) {
	return m_tempTiles.size();
}

int ParseTiles::getNumValidTiles(void) {
	return m_numTiles;
}

bool ParseTiles::openFile(void) {
	inFile.open(m_readFile);
	if(!inFile.is_open()) {
		return false;
	} else {
		return true;
	}
}

void ParseTiles::readData(void) {
	string tempName = "";
	int tempFlag = 0;
	int tempAmount = 0;
	int tempRandom = 0;
	int size = 0;

	getline(inFile, tempName, '#');
	while(!inFile.eof()) {
		inFile >> tempFlag;
		inFile >> tempAmount;
		inFile >> tempRandom;

		createTile(tempAmount,tempName,tempFlag,tempRandom);
		++m_numTiles;
		m_numAmount += tempAmount;
		
		inFile.ignore();
		getline(inFile, tempName, '#');
	}
	m_numTiles = m_numAmount;
}

void ParseTiles::closeFile(void) {
	inFile.close();
}

list<Tile> ParseTiles::getTileData(void) {
	return m_tempTiles;
}

bool ParseTiles::checkTiles(void) {
	
	if(m_tempTiles.size() != m_numTiles) {
		return false;
	} else {
		return true;
	}
}

//******************************	Private Functions	******************************

void ParseTiles::createTile(int a_tempAmount, string a_tempName, unsigned a_tempFlag, int a_tempRandom) {
	if(a_tempAmount <= 0) { // ERROR CHECKING, if amount is zero, program used to hang. Now it will return an error.
		////cout << "ERROR!!! A tile has ZERO for it's amount. Please check the file:" << endl;
		////cout << " -> <" << m_readFile << ">" << endl;
	} else {
		// Depending on the amount of tiles for each type, create a new tile in the list
		for(int i=0;i<a_tempAmount;++i) {
			// Pick a random background image for each tile
			int w = TILE_BG_WIDTH;
			int h = TILE_BG_HEIGHT;
			Position a_tempPosition((random()%3)*w,(random()%2)*h);
			m_tempTiles.push_back(Tile(a_tempFlag,a_tempName,a_tempPosition));
			// For each tile, set the exits
			if(a_tempRandom == 1)
				createRandomExits();
			if(a_tempRandom == 0)
				createExits();
		}
	}
}

void ParseTiles::createRandomExits(void) {
	int numExits = (random()%3)+1; //generate a number between 1 and 3
	int randExit = 0;

	for(int i=0;i<numExits;++i) {
		randExit = (random()%4)+1;
		
		switch(randExit) {
			case 1:
				if(!m_tempTiles.back().hasExit(UP)) {
					m_tempTiles.back().setExit(UP);
				}
				break;
			case 2:
				if(!m_tempTiles.back().hasExit(DOWN)) {
					m_tempTiles.back().setExit(DOWN);
				}
				break;
			case 3:
				if(!m_tempTiles.back().hasExit(LEFT)) {
					m_tempTiles.back().setExit(LEFT);
				}
				break;
			case 4:
				if(!m_tempTiles.back().hasExit(RIGHT)) {
					m_tempTiles.back().setExit(RIGHT);
				}
				break;
		}
	}
}

void ParseTiles::createExits(void) {
	if(m_tempTiles.back().getFlag() == 0) { //Start Tile: requires 3 exits
		m_tempTiles.back().setExit(UP);
		m_tempTiles.back().setExit(LEFT);
		m_tempTiles.back().setExit(RIGHT);
	}
	if(m_tempTiles.back().getFlag() == 13) { //tile doesn't require any exits
	}
	if(m_tempTiles.back().getFlag() == 16) { //tile doesn't require any exits
	}
}